package com.gxt.sysmanage.controller;

import com.gxt.sysmanage.service.IDictionaryitemService;
import com.gxt.sysmanage.domain.Dictionaryitem;
import com.gxt.sysmanage.query.DictionaryitemQuery;
import com.gxt.basic.utils.AjaxResult;
import com.gxt.basic.utils.PageList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dictionaryitem")
@Api(value = "数据字典详情controller",description = "数据字典详情controller")
public class DictionaryitemController {
    @Autowired
    public IDictionaryitemService dictionaryitemService;

    @PutMapping
    @ApiOperation(value = "保存数据字典详情")
    public AjaxResult save(@RequestBody Dictionaryitem dictionaryitem){
        try {
            if( dictionaryitem.getId()!=null)
                dictionaryitemService.update(dictionaryitem);
            else
                dictionaryitemService.insert(dictionaryitem);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }

    @DeleteMapping(value="/{id}")
    @ApiOperation(value = "保存数据字典详情")
    public AjaxResult removeById(@PathVariable("id") Long id){
        try {
            dictionaryitemService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }
	

    @GetMapping("/{id}")
    @ApiOperation(value = "查询数据字典详情")
    public AjaxResult loadById(@PathVariable("id")Long id)
    {
        try {
            Dictionaryitem dictionaryitem = dictionaryitemService.loadById(id);
            return AjaxResult.me().setResult(dictionaryitem);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！"+e.getMessage());
        }
    }


    @GetMapping
    @ApiOperation(value = "查询所有数据字典详情")
    public AjaxResult getAll(){

        try {
            List< Dictionaryitem> list = dictionaryitemService.loadAll();
            return AjaxResult.me().setResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！"+e.getMessage());
        }
    }



    @PostMapping
    @ApiOperation(value = "分页查询")
    public AjaxResult pageList(@RequestBody DictionaryitemQuery query)
    {
        try {
            PageList<Dictionaryitem> pageList = dictionaryitemService.pageList(query);
            return AjaxResult.me().setResult(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取分页数据失败！"+e.getMessage());
        }
    }

    @PatchMapping
    @ApiOperation(value = "批量删除数据字典详情")
    public AjaxResult batchDelete(@ApiParam(required = true) @RequestBody List<Long> ids){
        try {
            dictionaryitemService.batchDelete(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败!"+e.getMessage());
        }
        return AjaxResult.me();
    }

}
